<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title.sync="title" width="940px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="125px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="仓库名称" prop="warehousename">
            <el-input v-model="form.warehousename" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库地址" prop="warehouselocation">
            <el-input v-model="form.warehouselocation" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="仓库管理员" prop="personname">
            <el-input v-model="form.personname" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系方式" prop="warehouseadministratorphone">
            <el-input v-model="form.warehouseadministratorphone" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="仓库归属">
            <el-input v-model="form.warehousedeptname" class="input_text" :readonly="isView" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库经度">
            <el-input v-model="form.warehouselongitude" class="input_text" :readonly="isView" @keydown.native="numberKeyDown" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="仓库纬度">
            <el-input v-model="form.warehouselatitude" class="input_text" :readonly="isView" @keydown.native="numberKeyDown" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库状态">
            <el-select v-model="form.state" allow-create filterable default-first-option style="width:300px;" :disabled="isView">
              <el-option key="1" label="在用" value="1" />
              <el-option key="0" label="停用" value="0" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-show="isView===false || isAdd===true" type="text" @click="cancel">取消</el-button>
      <el-button v-show="isAdd===false && isView===true" :loading="loading" type="primary" @click="cancel">返回</el-button>
      <el-button v-show="isView===false" :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/tWarehouse'
import { validatePhone, validateNumber } from '@/utils/validate'
export default {
  props: {
    isView: {
      type: Boolean,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    },
    supThis: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      title: '',
      form: {
        id: '',
        warehousename: '',
        warehouselocation: '',
        warehousedeptid: '',
        warehousedeptname: '',
        warehouselatitude: '',
        warehouselongitude: '',
        warehouseadministratorid: '',
        personname: '',
        warehouseadministratorphone: '',
        areacode: '',
        state: ''
      },
      rules: {
        warehousename: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ],
        warehouselocation: [
          { required: true, message: '请输入仓库地址', trigger: 'blur' }
        ],
        personname: [
          { required: true, message: '请输入仓库管理员', trigger: 'blur' }
        ],
        warehouseadministratorphone: [
          { required: true, validator: validatePhone, message: '请输入正确联系方式', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    const explorer = navigator.userAgent
    if (explorer.indexOf('Chrome') >= 0) {
      this.style = 'width: 200px'
    } else {
      this.style = 'width: 172px'
    }
  },
  mounted: function() {
    this.title = this.isAdd ? '新增' : (this.isView === true ? '详情' : '编辑')
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.state === '在用') {
            this.form.state = 1
          } else if (this.form.state === '停用') {
            this.form.state = 0
          }
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        }
      })
    },
    doAdd() {
      add(this.form)
        .then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.$parent.init()
        })
        .catch(err => {
          this.loading = false
          console.log(err)
        })
    },
    doEdit() {
      edit(this.form)
        .then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.supThis.init()
        })
        .catch(err => {
          this.loading = false
          console.log(err)
        })
    },
    numberKeyDown(event) {
      validateNumber(event)
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        warehousename: '',
        warehouselocation: '',
        warehousedeptid: '',
        warehousedeptname: '',
        warehouselatitude: '',
        warehouselongitude: '',
        warehouseadministratorid: '',
        personname: '',
        warehouseadministratorphone: '',
        areacode: '',
        state: ''
      }
    }
  }
}
</script>

<style scoped>
.input_text {
  width: 300px;
}
</style>
